package view;

import interfaces.View;

import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;

import model.XMLWorker;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Comparator;

public class EventTable extends JScrollPane implements View {

	private static final long serialVersionUID = 1L;
	private JTable jTable = null;
	private XMLWorker worker = null;
	private String filter = null;  //  @jve:decl-index=0:
	private TableRowSorter<EventTableModel> sorter = null;
	RowFilter<EventTableModel, Object> rf = null;
	private EventTableModel model = null;
	private DetailEditor detailEditor = null;

	public EventTable(XMLWorker worker, DetailEditor d) {
		super();
		this.worker = worker;
		this.detailEditor = d;
		initialize();
	}
	
	public void info() {
		System.out.println(jTable.getColumnModel().getColumn(0).getWidth());
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(500, 410);
		this.setViewportView(getJTable());
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	public JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.setSize(new Dimension(495, 80));
			jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

				@Override
				public void valueChanged(ListSelectionEvent e) {
			        if (e.getValueIsAdjusting()) {
			            return;
			        }
					int row = jTable.getSelectedRow();
					if(row>=0){
						int id = Integer.valueOf(jTable.getValueAt(row, 0).toString());
						detailEditor.showEvent(worker.getEventByID(id));
					}
				}
				
			});
		}
		return jTable;
	}

	@Override
	public void refresh() {
		model = new EventTableModel(worker.getEvents(), worker.getTableTitles());
		jTable.setModel(model);
		jTable.getColumnModel().getColumn(0).setPreferredWidth(15);
		jTable.getColumnModel().getColumn(1).setPreferredWidth(55);
		jTable.getColumnModel().getColumn(2).setPreferredWidth(50);
		jTable.getColumnModel().getColumn(3).setPreferredWidth(310);
	}

	public void setFilter(String f, int row) {
		if(f.compareTo(XMLWorker.ALLE)!=0) {
			sorter = new TableRowSorter<EventTableModel>(model);
			jTable.setRowSorter(sorter);
	        try {
	            rf = RowFilter.regexFilter(f, row);
	        } catch (java.util.regex.PatternSyntaxException e) {
	            return;
	        }
	        sorter.setRowFilter(rf);
		} else {
			jTable.setRowSorter(null);
		}
        refresh();
	}

	public void test() {
        
	}
}
